package com.zyb.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.zyb.domain.Brand;
import com.zyb.domain.Goods;
import com.zyb.domain.Type;
import com.zyb.service.GoodsService;

@Controller
public class GoodsController {
	@Resource
	private GoodsService goodsService;
	
	@RequestMapping("list")
	public String list(Model m,Integer pageNum,Integer pageSize){
		if(pageNum==null){
			pageNum=1;
		}
		pageSize=5;
		PageInfo<Goods> info = goodsService.list(pageNum, pageSize);
		m.addAttribute("info", info);
		return "index";
	}
	
	@RequestMapping("toAdd")
	public String toAdd(){
		return "add";
	}
	@ResponseBody
	@RequestMapping("add")
	public boolean add(Goods goods,MultipartFile myFile){
		if(myFile!=null && !myFile.isEmpty()){
			String path="e:/pic/";
			String originalFilename = myFile.getOriginalFilename();
			String end = originalFilename.substring(originalFilename.lastIndexOf("."));
			String newName= UUID.randomUUID()+end;
			File file = new File(path,newName);
			try {
				myFile.transferTo(file);
				goods.setPicture(newName);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return goodsService.add(goods)>0;
		
	}
	@ResponseBody
	@RequestMapping("brands")
	public List<Brand> brands(){
		return goodsService.brands();
		
	}
	@ResponseBody
	@RequestMapping("types")
	public List<Type> types(){
		return goodsService.types();
		
	}
	
	@RequestMapping("toUpda")
	public String toUpda(Integer id,Model m){
		Goods goods=goodsService.toUpda(id);
		m.addAttribute("goods", goods);
		return "good";
		
	}
	@RequestMapping("upda")
	public String upda(Integer id,Model m){
		Goods goods=goodsService.toUpda(id);
		m.addAttribute("goods", goods);
		return "update";
		
	}
	
	@ResponseBody
	@RequestMapping("update")
	public boolean update(Goods goods,MultipartFile myFile){
		if(myFile!=null && !myFile.isEmpty()){
			String path="e:/pic/";
			String originalFilename = myFile.getOriginalFilename();
			String end = originalFilename.substring(originalFilename.lastIndexOf("."));
			String newName= UUID.randomUUID()+end;
			File file = new File(path,newName);
			try {
				myFile.transferTo(file);
				goods.setPicture(newName);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return goodsService.update(goods)>0;
		
	}
	
	@ResponseBody
	@RequestMapping("dele")
	public boolean dele(Integer id){
		return goodsService.dele(id)>0;
		
	}
}
